from marshmallow import fields, Schema


class ModelMeta(type):
    def __new__(cls, name, bases, attrs):
        _field_mapping = {}
        for key, value in attrs.items():
            if isinstance(value, fields.Field):
                _field_mapping[key] = value
        _schema = Schema.from_dict(_field_mapping)
        for key in _field_mapping:
            attrs.pop(key)
        attrs['_field_mapping'] = _field_mapping
        attrs['_schema'] = _schema()
        return super().__new__(cls, name, bases, attrs)


class Model(metaclass=ModelMeta):
    def __init__(self, *args, **kwargs):
        raw_data = dict(*args, **kwargs)
        field_data = self._schema.load(raw_data)
        for key, value in field_data.items():
            setattr(self, key, value)
